<?php

namespace App\Http\Requests\Form;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class PmFormRequest extends FormRequest
{
    /**
     * 表示验证器是否应在第一个规则失败时停止
     *
     * @var bool
     */
    protected $stopOnFirstFailure = true;

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'title' => 'required|max:64',
            'form_items' => [
                'required',
                function (string $attribute, mixed $value, \Closure $fail) {
                    if (! is_array($value)) {
                        $fail('表单项必须为数组');
                    }
                },
            ],
        ];
    }

    /**
     * 获取验证错误的自定义属性
     *
     * @return string[]
     */
    public function attributes()
    {
        return [
            'title' => '表单标题',
            'form_items' => '表单项',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息
     *
     * @return string[]
     */
    public function messages()
    {
        return [
            'title.required' => '表单标题不能为空',
            'title.max' => '表单标题最长为64个字符',
            'form_items.required' => '表单项目不能为空',
        ];
    }
}
